package com.company.homework;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class CreateEquation{
    /**
     * 根据命令生成一系列表达式存入文件
     * @throws IOException IO异常
     */
    public static File  createEquation() throws IOException {
        System.out.println("请输入需要存放生成表达式的文件名");

        Scanner input=new Scanner(System.in);
        String path=input.next();

        System.out.println("要生成正确的表达式输入1，生成不正确的输入2");
        int sign=input.nextInt();

        System.out.println("请输入要生成的表达式的数量");
        int number=input.nextInt();
        input.close();

        File file=new File(path);
        file.createNewFile();
        PrintWriter output=new PrintWriter(file);

        for(int i=0;i<number;i++){
            if(sign==1){
                output.println(right());
            }else {
                output.println(wrong());
            }
        }

        output.close();

        return file;
    }

    /**
     * 生成一条正确的表达式
     * @return 正确的表达式
     */
    private static String right(){
        List<String> list=new LinkedList<>();
        Random random=new Random();

        //生成主表达式(不含括号)
        int expressionLength=random.nextInt(8)*2+1;
        for(int i=0;i<expressionLength;i++){
            if(i%2==0){
                list.add(random.nextInt(100)+1+"");
            }else {
                list.add(randomOperator()+"");
            }
        }

        //插入括号到表达式中:
        int number=random.nextInt(5);
        int[] left=new int[number];
        int[] right=new int[number];

        //计算出括号插入位点
        for(int i=0;i<number;i++){
            int l=random.nextInt(expressionLength/2+1)*2;
            int r=random.nextInt(expressionLength/2-l/2+1)*2+l;
            left[i]=l;
            right[i]=r;
        }

        Arrays.sort(left);
        Arrays.sort(right);

        StringBuilder builder=new StringBuilder();
        for(int i=0,j=0,k=0;i<list.size();i++){
            while (j<left.length&&left[j]==i){
                builder.append('(');
                j++;
            }

            builder.append(list.get(i));

            while (k<right.length&&right[k]==i){
                builder.append(')');
                k++;
            }
        }

        return builder.toString();
    }

    /**
     * 生成一条错误的表达式
     * @return 错误的表达式
     */
    private static String wrong(){
        String transition=right();
        Random random=new Random();

        StringBuilder builder=new StringBuilder();

        //生成要产生错误的位点
        int[] point=new int[random.nextInt(2)+3];
        for(int i=0;i<point.length;i++){
            point[i]=random.nextInt(transition.length());
        }

        Arrays.sort(point);

        //制作错误
        for(int i=0,j=0;i<transition.length();i++){
            while (j<point.length&&point[j]==i){
                if(Character.isDigit(transition.charAt(i))){
                    if(random.nextInt(1)==0){
                        builder.append(randomOperator());
                    }else if(random.nextInt(1)==0) {
                        builder.append('(');
                    }else{
                        builder.append(')');
                    }
                }else {
                    builder.append(random.nextInt(10));
                }

                builder.append(randomOperator());

                j++;
                continue;
            }

            builder.append(transition.charAt(i));
        }

        return builder.toString();
    }

    /**
     * 随机生成一个运算符
     * @return 运算符
     */
    private static char randomOperator(){
        Random random=new Random();
        int n=random.nextInt(4);

        switch (n){
            case 0:return '+';
            case 1:return '-';
            case 2:return '*';
            case 3:return '/';
        }

        return '$';
    }
}